package com.aaa.fpa.service;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.fpa.dao.TesterSasDao;
import com.aaa.fpa.dao.ViewTestResultSasDao;
import com.aaa.pojo.MyReturn;
import com.aaa.pojo.TesterFpa;
import com.aaa.pojo.TesterVo;
import com.aaa.pojo.ViewTestResultSas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @projectName: 20220622springmvc
 * @package: com.aaa.service.impl
 * @className: TestResultSasServiceImpl
 * @author: HH
 * @description: TODO
 * @date: 2022/7/15 21:04
 * @version: 1.0
 */
@Service
public class TestResultSasServiceImpl implements TestResultSasService {
    @Autowired
    private TesterSasDao testerSasDao;
    @Autowired
    private ViewTestResultSasDao viewTestResultSasDao;

    MyReturn myReturn=new MyReturn();
    @Override
    public String saveTestResult(List<Map<String,Object>> answerList, TesterVo tester) {
        //保存测试者信息
        TesterFpa testerFpa= new TesterFpa();
        //Bean之间的转换主要是相同属性的复制
        /**
         * 复制Bean对象属性<br>
         * 限制类用于限制拷贝的属性，例如一个类我只想复制其父类的一些属性，就可以将editable设置为父类
         *
         * @param source           源Bean对象
         * @param target           目标Bean对象
         * @param ignoreProperties 不拷贝的的属性列表
         */
        BeanUtil.copyProperties(tester,testerFpa,"id");
        testerSasDao.insert(testerFpa);
        //获取自增之后的主键
        Integer testerId = testerFpa.getId();
        //保存测试结果信息
        int i = this.insertBatch(answerList, testerId);
        if(i>0){
            return this.seleResult(testerId);
        }
        return null;
    }

    @Override
    public int insertBatch(List<Map<String,Object>> answerList, Integer testerId) {
        return testerSasDao.insertBatch(answerList,testerId);
    }
    @Override
    public String seleResult(Integer testerId) {

        ViewTestResultSas viewSasSds = viewTestResultSasDao.selectResultSas(testerId);
        double a = viewSasSds.getForward() + viewSasSds.getInversion();
        if (a <= 50) {
            return "/front/SasNormal.jsp";
        } else if (a < 60) {
            return "/front/SasMild.jsp";
        } else if (a < 70) {
            return "/front/SasModerate.jsp";
        } else {
            return "/front/SasSevere.jsp";
        }
    }
}
